import fs, { existsSync, readFileSync } from "fs";
import { join } from "path";
import { PATH, cmdExec, isDev } from "../utils";
import { appListCmd } from "./cmdConfig";

const getAppListFile = async (SN, filePath) => {
  const editorToolAppPath = join(process.cwd(), isDev() ? "./plugins/" : "./");
  let appPath = join(editorToolAppPath || "", "entry-default-signed.hap");
  console.log("**********2**************" + appPath);
  let grepEditorApp = await cmdExec(appListCmd.hdc.grepEditorApp(SN));
  console.log("**********3**************" + appListCmd.hdc.grepEditorApp(SN));
  while (grepEditorApp.search("com.ohos.editorapp") != -1) {
    await cmdExec(appListCmd.hdc.unstallEditorTool(SN));
    grepEditorApp = await cmdExec(appListCmd.hdc.grepEditorApp(SN));
    console.log("**********4**************" + grepEditorApp);
  }
  //安装应用
  await cmdExec(appListCmd.hdc.installEditorTool(SN, appPath));
  let result = await cmdExec(appListCmd.hdc.grepEditorApp(SN));
  console.log("**********5**************" + result); /*
  while(result.search("com.ohos.editorapp")==-1){
    await cmdExec(appListCmd.hdc.installEditorTool.format({appPath:appPath}));
    result=await cmdExec(appListCmd.hdc.grepEditorApp);
    console.log("**********6**************"+result)
  }*/
  //创建目录，赋权
  await cmdExec(appListCmd.hdc.mkdir(SN));
  await cmdExec(appListCmd.hdc.grantPermissions(SN));
  console.log("**********7**************");
  await cmdExec(appListCmd.hdc.getAllAppList(SN));
  console.log("**********8**************");
  await cmdExec(appListCmd.hdc.fileRecv(SN, filePath));
  console.log("**********9**************");
};/*
let timer;
const startWatch = (dataPath) => {
  stopWatch();
  timer = setInterval(async () => {
    if (!existsSync(dataPath)) {
      await getAppListFile();
    } else {
      stopWatch();
    }
  }, 500);
};
const stopWatch = () => {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
};*/

export const getAllAppListByHdc = async (SN) => {
  let appList = new Array();
  console.log("SN: "+SN);
  const SNPath=SN.replaceAll(":","_");
  const dataRootPath=PATH.dataPath;
  if(!fs.existsSync(dataRootPath)){
    if(!fs.existsSync(`D:\\ProgramData`)){
      fs.mkdirSync(`D:\\ProgramData`)
    }
    fs.mkdirSync(dataRootPath);
  }
  const filePath = join(dataRootPath, SNPath || "");
  if (!fs.existsSync(filePath)) {
    fs.mkdirSync(filePath);
  }
  const dataPath = join(filePath, "appnames.txt");
  console.log(dataPath);
  //startWatch(dataPath);
  do {
    console.log("**********1**************");
    await getAppListFile(SN, filePath);
    console.log("**********8**************");
  } while (!existsSync(dataPath));
  try {
    console.log(dataPath);
    let data = readFileSync(dataPath, { encoding: "utf-8" });
    var jsonobj = JSON.parse(data);
    let appInfoList = jsonobj.allAppList;
    if (appInfoList.length > 0) {
      appInfoList.forEach((element) => {
        let appInfoArray = new Array();
        appInfoArray.push(element.icon, element.name, element.package);
        appList.push(appInfoArray);
      });
    }
  } catch (err) {
    console.log("err*********" + err);
  }
  return appList;
};
